/** @format */

import Vue from "vue";
import VueRouter from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import Cookie from "js-cookie";
Vue.use(VueRouter);

const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};
const routes = [
  {
    path: "/",
    name: "Home",
    component: () => import("@views/Home/Home.vue")
  },
  {
    path: "/Class",
    name: "Class",
    component: () => import("@views/Class/Class.vue")
  },
  {
    path: "/ShopCart",
    name: "ShopCart",
    component: () => import("@views/ShopCart/ShopCart.vue")
  },
  {
    path: "/UserCenter",
    name: "UserCenter",
    component: () => import("@views/User/UserCenter.vue"),
    beforeEnter: (to, from, next) => {
      console.log(to, from);
      if (to.name == "UserCenter") {
        if (Cookie.get("token")) {
          console.log(1);
          next();
        } else {
          console.log(2);
          // next("/UserCenter/login");
          next("/UserCenter/login");
        }
      }
    }
  },
  {
    path: "/UserCenter/login",
    name: "login",
    component: () => import("@views/User/login/login.vue"),
    meta: {
      isShow: true
    }
  },
  {
    path: "/Products",
    name: "Products",
    component: () => import("@views/Products/Products.vue")
  },
  {
    path: "/search",
    name: "Search",
    component: () => import("@views/Home/Search.vue")
  },
  {
    path: "/finds",
    name: "Finds",
    component: () => import("@views/Home/finds.vue")
  }
];

const router = new VueRouter({
  routes
});
router.beforeEach((to, from, next) => {
  NProgress.start();
  next();
});
router.afterEach(() => {
  NProgress.done();
  // next();
});
export default router;
